package com.wl.pet.service.impl;

import com.wl.basic.service.IBaiduAiAuditService;
import com.wl.basic.util.LoginContext;
import com.wl.org.domain.Employee;
import com.wl.org.mapper.EmployeeMapper;
import com.wl.pet.domain.Pet;
import com.wl.pet.domain.PetDetail;
import com.wl.pet.domain.PetOnlineAuditLog;
import com.wl.pet.mapper.PetDetailMapper;
import com.wl.pet.mapper.PetMapper;
import com.wl.pet.mapper.PetOnlineAuditLogMapper;
import com.wl.pet.service.IPetService;
import com.wl.basic.service.impl.BaseServiceImpl;
import com.wl.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jws.Oneway;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Override
    public void save(Pet pet) {
        super.save(pet);
        //如果详情不等于空
        if(pet.getPetDetail()!=null){
            //获取详情
            PetDetail petDetail = pet.getPetDetail();
            petDetail.setPetId(pet.getId());
            petDetailMapper.save(petDetail);
        }
    }

    @Override
    public void remove(Long id) {
        super.remove(id);
        petDetailMapper.removeById(id);
    }

    @Override
    public void update(Pet pet) {
        super.update(pet);
        if(pet.getPetDetail()!=null){
            //获取详情
            PetDetail petDetail = pet.getPetDetail();
            petDetail.setPetId(pet.getId());
            petDetailMapper.update(petDetail);
        }
    }
    //上架
    @Override
    public void onsale(List<Long> ids, HttpServletRequest request) {
        //遍历结合
        for (Long id : ids) {
            //查询宠物id
            Pet pet = petMapper.loadById(id);
            //调用审核文本的工具类
            Boolean aBoolean = baiduAiAuditService.textAudit(pet.getName());
            //调用审核图片的工具类
            Boolean aBoolean1 = baiduAiAuditService.imageAudit(pet.getResources());
            //判断是否成功
            if (aBoolean && aBoolean1){
                //修改状态为1
                pet.setState(1);
                //设置上架时间
                pet.setOnsaletime(new Date());
                //
                petMapper.update(pet);
                //设置上架日志
                PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                petOnlineAuditLog.setPetId(id);
                petOnlineAuditLog.setState(1);
                //通过上下文对象获取登录人信息
                Logininfo logininfo = LoginContext.getLogininfo(request);
                Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());

                petOnlineAuditLog.setNote("审核成功");
                petOnlineAuditLog.setAuditId(employee.getId());
                petOnlineAuditLogMapper.save(petOnlineAuditLog);
            }else {
                //设置上架日志
                PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                petOnlineAuditLog.setPetId(id);
                petOnlineAuditLog.setState(1);
                //通过上下文对象获取登录人信息
                Logininfo logininfo = LoginContext.getLogininfo(request);
                Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());

                petOnlineAuditLog.setNote("审核失败");
                petOnlineAuditLog.setAuditId(employee.getId());
                petOnlineAuditLogMapper.save(petOnlineAuditLog);
            }
        }
    }
    //下架
    @Override
    public void offsale(List<Long> ids) {
        //
        petMapper.offsale(ids,new Date());
    }
}
